<?php


namespace catchAdmin\customs\controller;


use app\Request;
use catchAdmin\customs\model\Order as OrderModel;
use catchAdmin\customs\model\OrderSon as OrderSonModel;
use catcher\CatchAuth;
use catcher\CatchResponse;

class Index
{
    protected $orderModel;
    protected $OrderSonModel;

    /**
     *
     * @time 2021/11/03 14:02
     * @param OrderModel $orderModel
     * @param OrderSonModel $OrderSonModel
     * @return mixed
     */
    public function __construct(OrderModel $orderModel, OrderSonModel $OrderSonModel)
    {
        $this->orderModel = $orderModel;
        $this->OrderSonModel = $OrderSonModel;
    }

    /**
     * 订单列表
     *
     * @time 2020年01月09日
     * @param CatchRequest $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function statistics(CatchAuth $auth, Request $request)
    {
        $user = $auth->user();
        $baseData = [];$lineChartData=[];$barChartData=[];
        $yesterday = date("Y-m-d",strtotime("-1 day"));
        $yesterdays = date("Ymd",strtotime("-1 day"));
        $days = date("t");
        $o_month = date('Y-m');
        $c_month = date('Ym');
        //非管理员类型需要带商户号
        if($user['type'] !== 1){
            $orders = $this->orderModel->where([['deleteStatus','=',0],['status','=',0],['orderDate','like',"$o_month%"],['tenant_id','=',$user['tenant_id']]])->order('orderDate asc')->select()->toArray();
            $baseData['totalOrders'] = $this->orderModel->where([['deleteStatus','=',0],['status','=',0],['tenant_id','=',$user['tenant_id']]])->count();
            $baseData['yesterdayOrder'] = $this->orderModel->where([['deleteStatus','=',0],['status','=',0],['orderDate','like',"$yesterday%"],['tenant_id','=',$user['tenant_id']]])->count();
            $clears = $this->orderModel->where([['deleteStatus','=',0],['status','=',0],['push_inventory','=',800],['completeDateTime','like',"$c_month%"],['tenant_id','=',$user['tenant_id']]])->order('completeDateTime asc')->select()->toArray();
            $baseData['totalClears'] = $this->orderModel->where([['deleteStatus','=',0],['status','=',0],['push_inventory','=',800],['tenant_id','=',$user['tenant_id']]])->count();
            $baseData['yesterdayClear'] = $this->orderModel->where([['deleteStatus','=',0],['status','=',0],['push_inventory','=',800],['completeDateTime','like',"$yesterdays%"],['tenant_id','=',$user['tenant_id']]])->count();
        }else{
            $orders = $this->orderModel->where([['deleteStatus','=',0],['status','=',0],['orderDate','like',"$o_month%"]])->order('orderDate asc')->select()->toArray();
            $baseData['totalOrders'] = $this->orderModel->where([['deleteStatus','=',0],['status','=',0]])->count();
            $baseData['yesterdayOrder'] = $this->orderModel->where([['deleteStatus','=',0],['status','=',0],['orderDate','like',"$yesterday%"]])->count();
            $clears = $this->orderModel->where([['deleteStatus','=',0],['status','=',0],['push_inventory','=',800],['completeDateTime','like',"$c_month%"]])->order('completeDateTime asc')->select()->toArray();
            $baseData['totalClears'] = $this->orderModel->where([['deleteStatus','=',0],['status','=',0],['push_inventory','=',800]])->count();
            $baseData['yesterdayClear'] = $this->orderModel->where([['deleteStatus','=',0],['status','=',0],['push_inventory','=',800],['completeDateTime','like',"$yesterdays%"]])->count();
        }
        $baseData['toMonthOrder'] = count($orders);//本月订单
        $baseData['toMonthClear'] = count($clears);//本月清单
        //年度统计
        $month_now=date('n');
        $year_now=date('Y');
        for($i=0;$i<12;$i++){
            if($i+1<10){
                $month = $year_now.'-0'.($i+1);
                $barChartData['times'][] = $month;
            }else{
                $month = $year_now.'-'.($i+1);
                $barChartData['times'][] = $month;
            }
            if($i<$month_now){
                if($user['type'] !== 1){
                    $where = [];
                    $where[] = ['deleteStatus','=',0];
                    $where[] = ['status','=',0];
                    $where[] = ['orderDate','like',"$month%"];
                    $where[] = ['tenant_id','=',$user['tenant_id']];
                    $barChartData['orders'][] = $this->orderModel->where($where)->count();
                    $where = [];
                    $where[] = ['deleteStatus','=',0];
                    $where[] = ['status','=',0];
                    $where[] = ['push_inventory','=',800];
                    $month = str_replace('-','',$month);
                    $where[] = ['completeDateTime','like',"$month%"];
                    $where[] = ['tenant_id','=',$user['tenant_id']];
                    $barChartData['clears'][] = $this->orderModel->where($where)->count();
                }else{
                    $where = [];
                    $where[] = ['deleteStatus','=',0];
                    $where[] = ['status','=',0];
                    $where[] = ['orderDate','like',"$month%"];
                    $barChartData['orders'][] = $this->orderModel->where($where)->count();
                    $where = [];
                    $where[] = ['deleteStatus','=',0];
                    $where[] = ['status','=',0];
                    $where[] = ['push_inventory','=',800];
                    $month = str_replace('-','',$month);
                    $where[] = ['completeDateTime','like',"$month%"];
                    $barChartData['clears'][] = $this->orderModel->where($where)->count();
                }
            }
        }

        $chartData = [];
        for ($i = 1; $i <= $days; $i++){
            $chartData[$i.'日']['orders'] = 0;
            $chartData[$i.'日']['clears'] = 0;
            $day = $i>=10 ? $i : "0".$i;
            foreach ($orders as $key => $value){
                if(strpos($value['orderDate'],$o_month.'-'.$day) === 0){
                    $chartData[$i.'日']['orders']++;
                    unset($orders[$key]);
                }else{
                    break 1;
                }

            }
            foreach ($clears as $key => $value){
                if(strpos($value['completeDateTime'],$c_month.$day) === 0){
                    $chartData[$i.'日']['clears']++;
                    unset($clears[$key]);
                }else{
                    break 1;
                }
            }
        }
        $baseData['todayOrder'] = $chartData[(int)date('d').'日']['orders'];//本日订单
        $baseData['todayClear'] = $chartData[(int)date('d').'日']['clears'];//本日清单
        $lineChartData['times'] = array_keys($chartData);
        $lineChartData['orders'] = array_column($chartData,'orders');
        $lineChartData['clears'] = array_column($chartData,'clears');
        $data['baseData'] = $baseData;
        $data['lineChartData'] = $lineChartData;
        $data['barChartData'] = $barChartData;
        return CatchResponse::success($data, '成功');
    }
}